﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using DebIt.Common.Model;

namespace DebIt.ViewModel
{
    public class MainPageViewModel : ViewModelBase
    {
        #region variables

        private ObservableCollection<Client> _clients = new ObservableCollection<Client>();
        private ObservableCollection<Loan> _loans = new ObservableCollection<Loan>();

        #endregion


        #region constructor

        public MainPageViewModel()
        {
            Service.NewClientAddedEvent += ServiceNewClientAddedEvent;
            Service.ClientDeletedEvent += ServiceClientDeletedEvent;
            Service.NewLoanAddedEvent += ServiceNewLoanAddedEvent;
            Service.LoanDeletedEvent += ServiceLoanDeletedEvent;
        }

        ~MainPageViewModel()
        {
            Service.NewClientAddedEvent -= ServiceNewClientAddedEvent;
            Service.ClientDeletedEvent -= ServiceClientDeletedEvent;
            Service.NewLoanAddedEvent -= ServiceNewLoanAddedEvent;
            Service.LoanDeletedEvent -= ServiceLoanDeletedEvent;
        }

        #endregion


        #region properties

        public ObservableCollection<Client> Clients
        {
            get { return _clients ?? (_clients = new ObservableCollection<Client>()); }
        }

        public ObservableCollection<Loan> Loans
        {
            get { return _loans ?? (_loans = new ObservableCollection<Loan>()); }
        }

        #endregion


        #region methods

        protected override void Load()
        {
            Service.GetAllClients().ForEach(x => Clients.Add(x));
            Service.GetAllLoans().ForEach(x => Loans.Add(x));
        }

        void ServiceLoanDeletedEvent(object sender, Loan loan)
        {
            _loans.Remove(loan);
        }

        void ServiceNewLoanAddedEvent(object sender, Loan loan)
        {
            _loans.Add(loan);
        }

        void ServiceClientDeletedEvent(object sender, Client client)
        {
            _clients.Remove(client);
        }

        void ServiceNewClientAddedEvent(object sender, Client client)
        {
            _clients.Add(client);
        }

        #endregion


        #region commands

        #region clients
        
        #region delete client command

        private ICommand _deleteClientCommand;

        public ICommand DeleteClientCommand
        {
            get
            {
                return _deleteClientCommand ?? (_deleteClientCommand = new PhoneCommand<Client>(
                                                                     Delete,
                                                                     CanDelete
                                                                     ));
            }
        }

        private void Delete(Client client)
        {
            Service.Delete(client);
        }

        private bool CanDelete(Client parameter)
        {
            return true;
        }

        #endregion
        
        #endregion

        #region loan

        #region delete loan command

        private ICommand _deleteLoanCommand;

        public ICommand DeleteLoanCommand
        {
            get
            {
                return _deleteLoanCommand ?? (_deleteLoanCommand = new PhoneCommand<Loan>(
                                                                     Delete,
                                                                     CanDelete
                                                                     ));
            }
        }

        private void Delete(Loan loan)
        {
            Service.Delete(loan);
        }

        private bool CanDelete(Loan loan)
        {
            return true;
        }

        #endregion

        #endregion


        #region client loan state

        #region loan isPaid changed command

        private ICommand _isPaiedChangedCommand;

        public ICommand IsPaiedChangedCommand
        {
            get
            {
                return _isPaiedChangedCommand ?? (_isPaiedChangedCommand = new PhoneCommand<ClientLoanState>(
                                                                     ChangePaid,
                                                                     CanDelete
                                                                     ));
            }
        }

        private void ChangePaid(ClientLoanState cls)
        {
            Service.Update(cls);
        }

        private bool CanDelete(ClientLoanState cls)
        {
            return true;
        }

        #endregion

        #endregion

        #endregion
    }
}
